package com.chaoyong.higo.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.FragAllGoodsAdapter.OnCartClick;
import com.chaoyong.higo.bean.ListEntity;
import com.chaoyong.higo.cainiaowo.CartProvider;
import com.chaoyong.higo.utils.TransferTool;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SearchAdapter extends BaseAdapter {
	
	private List<ListEntity> data;
	private Activity act;
	private LayoutInflater inflater;
	
	/** 商品图片 */
	private ImageView itemshop_image_img;
	/** 商品名称 */
	private TextView itemshops_name_tx;
	/** 商品价格 */
	private TextView itemshops_price_tx;
	/** 已参与多少人次 */
	private TextView itemshops_count_tx;
	/** 总共需要多少人次 */
	private TextView itemshops_total_tx;
	/** 还剩多少人次 */
	private TextView itemshops_left_tx;
	/** 进度条 */
	private ProgressBar test_progressbar;
	/** 添加到购物车 */
	private ImageView itemshop_shop_imgbt;
	// private String id;
	private OnCartClick2 click;
	
	private ImageView[] ivs = new ImageView[100];
	
	private ListEntity entity;
	private CartProvider cartProvider;
	
	public SearchAdapter(Activity act, List<ListEntity> data) {
		// TODO Auto-generated constructor stub
		this.act = act;
		this.data = data;
		cartProvider = new CartProvider(act);
		inflater = LayoutInflater.from(act);
	}
	
	public void changeDatas(List<ListEntity> data2){
		this.data = data2;
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_shops, null);
			itemshop_image_img = ViewHolder.get(convertView, R.id.itemshop_image_img);
			itemshops_name_tx = ViewHolder.get(convertView, R.id.itemshops_name_tx);
			itemshops_price_tx = ViewHolder.get(convertView, R.id.itemshops_price_tx);
			itemshops_count_tx = ViewHolder.get(convertView, R.id.itemshops_count_tx);
			itemshops_total_tx = ViewHolder.get(convertView, R.id.itemshops_total_tx);
			itemshops_left_tx = ViewHolder.get(convertView, R.id.itemshops_left_tx);
			test_progressbar = ViewHolder.get(convertView, R.id.test_progressbar);
			itemshop_shop_imgbt = ViewHolder.get(convertView, R.id.itemshop_shop_imgbt);
			
			//final String uri = data.get(position).getFace_img().get(0);
			//ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uri, itemshop_image_img);
			final String uri = data.get(position).getCover_img().split("\\|")[0];
			ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uri, itemshop_image_img);
			// 价格
			Double goods_price = data.get(position).getGoods_price();
			itemshops_price_tx.setText(goods_price + "");
			// 名称
			String goods_name = data.get(position).getGoods_name();
			itemshops_name_tx.setText("(第" + data.get(position).getExpect() + act.getResources().getString(R.string.expect) + ")" + goods_name);
			// 总共
			String totals = TransferTool.getString(goods_price + "", ".");
			itemshops_total_tx.setText(totals);
			// 已参与
			int count_tx = data.get(position).getSurplus();
			itemshops_count_tx.setText(count_tx + "");
			// 还剩
			int left_tx = TransferTool.Strtoint(goods_price + "", ".") - count_tx;
			itemshops_left_tx.setText(left_tx + "");
			Double d = (double) (count_tx / (TransferTool.Strtoint(goods_price + "", ".") / 100.00));
			//Double double1 = (double) (listr.get(arg1).getSurplus()) / (TransferTool.toInt(total) / 100);
			//Double d = (double) (count_tx / Double.parseDouble(totals));
			if(d < 1 && d > 0){
				test_progressbar.setProgress(1);
			}else{
				test_progressbar.setProgress(d.intValue());
			}
			ivs[position] = itemshop_image_img;
			//商品期数
//			final String expect = mList.get(position).getExpect();

			itemshop_shop_imgbt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					cartProvider.put(data.get(position));
					// Toast.makeText(mContext, "已加入到购物车",
					// Toast.LENGTH_SHORT).show();
					click.onCartClick(data.get(position).getId(),data.get(position).getExpect(),data.get(position));
//					entity = mList.get(position);
					//FragAllGoods_z.onAnimImage(uri);
				}
			});
		}
		return convertView;
	}
	
	public void setOnCartClickListener(OnCartClick2 click) {
		this.click = click;
	}

	public interface OnCartClick2 {
		public void onCartClick(String id,String expect,Serializable sb);
	}

	
}
